/**
 * Created by Administrator on 2016/8/25.
 */

var wsServer = 'ws://123.123.123.123:9999';
var websocket = new WebSocket(wsServer);
//readyState
var repeat_conn = null;
var conn_keepalive = null;

websocket.onopen = function (evt) {
    var uid = jq("BODY").attr('current_uid');
    var link_map = new Object();
    link_map.action = 'online';
    link_map.data = new Object();
    link_map.data.uid = uid;
    websocket.send(jq.toJSON(link_map));
    conn_keepalive = setInterval(function(){
        websocket.send('');
    },2000);
};

websocket.onclose = function (evt) {
    clearInterval(conn_keepalive);
    alert('网络链接不稳定，请刷新页面。');
};

websocket.onmessage = function (evt){
    var data = eval("("+ evt.data +")");
    var action = data['action'];
    var return_data = data.data;

    if(typeof chat[action] == "function"){
        chat[action](return_data);
    }else{
        console.log(action);
        alert("错误，请联系管理员！");
    }
}

var jq = jQuery.noConflict();
jq(document).ready(function(){
    jq("#push_msg").bind('click',chat['push_msg']);
});




